package handlers

import (
	"fmt"

	"github.com/kataras/iris/v12"
	"github.com/qasaur/gremgo"
)

var gremlinClient gremgo.Client

func init() {

	dialer := gremgo.NewDialer("ws://192.168.10.160:8182")
	errs := make(chan error)
	gremlinClient, _ = gremgo.Dial(dialer, errs)
}

func gremlinExecutorWrapper(database string, statement string, bindings map[string]string, rebindings map[string]string) (resp interface{}, err error) {
	_statement := fmt.Sprintf(`g=ConfiguredGraphFactory.open("%s").traversal();%s`, database, statement)
	return gremlinClient.Execute(_statement, bindings, rebindings)
}

func gremlinGraphExecutorWrapper(database string, statement string, bindings map[string]string) (resp interface{}, err error) {
	return gremlinExecutorWrapper(database, statement, bindings, map[string]string{})
}

func gremlinEdgeFormatter(vertex map[string]interface{}) {

}

func gremlinVertexFormatter(edge map[string]interface{})

// func GremlinRawQueryHandler() {

// }

func GremlinGetVertexHandler(ctx iris.Context) {

	statement := `g.V().bothE().as("e").otherV().as("v").select("v", "e").limit(2)`

	resp, err := gremlinGraphExecutorWrapper("yelp", statement, map[string]string{})

	if err != nil {
		fmt.Println(err)
	}

	ctx.JSON(resp)

}

// func GremlinGetEdgesHandler() {

// }

// func GremlinGraphExpandHandler() {

// }
